package org.chen.auth.controller;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.chen.auth.service.UserService;
import org.chen.common.auth.dto.LoginRequest;
import org.chen.common.auth.dto.RegisterRequest;
import org.chen.common.exception.BusinessException;
import org.chen.common.result.ResultVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@Tag(name = "用户信息管理", description = "用户信息管理")
public class LoginController {
    @Resource
    private UserService userService;

    @PostMapping("/login")
    @Operation(summary = "登录接口")
    public ResultVO login(@Valid @RequestBody LoginRequest loginRequest) {
        return userService.login(loginRequest);
    }

    @PostMapping("/register")
    @Operation(summary = "注册接口")
    public ResultVO register(@Valid @RequestBody RegisterRequest registerRequest) {
        return userService.register(registerRequest);
    }

    @PostMapping("/logout")
    @Operation(summary = "退出接口")
    public ResultVO logout(){
       return userService.logout();
    }

    @GetMapping("/userinfo")
    @Operation(summary = "获取用户信息", description = "获取用户信息")
    public ResultVO userinfo(){
        try {
            Object loginId = StpUtil.getLoginId();
            if (loginId == null) {
                return ResultVO.fail("未登录或登录已过期");
            }

            // 获取用户信息 - 使用当前登录用户的token
            String token = StpUtil.getTokenValue();

            // 获取用户信息
            Map<String, Object> userInfo = userService.getUserInfo(token);

            return ResultVO.success(userInfo);
        } catch (Exception e) {
            log.error("获取用户信息失败: {}", e.getMessage());
            return ResultVO.fail("获取用户信息失败" + e.getMessage());
        }
    }

    /**
     * 刷新token
     */
    @PostMapping("/refresh-token")
    @Operation(summary = "刷新token", description = "刷新token的方法")
    public ResultVO refreshToken() {
        try {
            String currentToken = StpUtil.getTokenValue();
            if (currentToken == null || currentToken.isEmpty()) {
                return ResultVO.fail("当前无有效token");
            }

            String newToken = userService.refreshToken(currentToken);

            Map<String, String> result = new HashMap<>();
            result.put("token", newToken);

            return ResultVO.success(result);
        } catch (Exception e) {
            log.error("刷新token失败: {}", e.getMessage());
            return ResultVO.fail("刷新token失败" + e.getMessage());
        }
    }

    /**
     * 检查token状态
     */
    @GetMapping("/token/check")
    @Operation(summary = "检查token状态", description = "检查token的状态")
    public ResultVO checkToken() {
        try {
            boolean isLogin = StpUtil.isLogin();
            if (!isLogin) {
                return ResultVO.fail("未登录");
            }

            Map<String, Object> tokenInfo = new HashMap<>();
            tokenInfo.put("isLogin", true);
            tokenInfo.put("loginId", StpUtil.getLoginId());
            tokenInfo.put("tokenValue", StpUtil.getTokenValue());
            tokenInfo.put("tokenTimeout", StpUtil.getTokenTimeout());

            return ResultVO.success(tokenInfo);
        } catch (Exception e) {
            log.error("检查token状态失败: {}", e.getMessage());
            return ResultVO.fail("检查token状态失败: " + e.getMessage());
        }
    }
}
